﻿using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;

namespace CoreModule
{
    /// <summary>
    /// 计算机信息
    /// </summary>
    public static class Computer
    {

        /// <summary>
        /// 获取IP
        /// </summary>
        /// <returns></returns>
        public static List<string> GetIPList()
        {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            int len = interfaces.Length;

            var list = new List<string>();

            for (int i = 0; i < len; i++)
            {
                NetworkInterface ni = interfaces[i];

                IPInterfaceProperties property = ni.GetIPProperties();
                 
                foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
                    || ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                    {
                        list.Add(ip.Address.ToString());
                    }
                }
            }
            return list;
        }

        /// <summary>
        ///  获取Mac
        /// </summary>
        /// <returns></returns>
        public static List<string> GetMacList()
        {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            int len = interfaces.Length;

            var list = new List<string>();

            for (int i = 0; i < len; i++)
            {
                NetworkInterface ni = interfaces[i];

                IPInterfaceProperties property = ni.GetIPProperties();

                var mac = ni.GetPhysicalAddress().ToString();

                foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
                    || ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                    {

                        list.Add(mac);
                    }
                }
            }
            return list.Distinct().ToList();
        }
    }
}
 
